package com.dy.placeOrder.controller;


import com.dy.placeOrder.service.ConfirmOrderService;
import com.dy.placeOrder2.service.OrdersService;
import com.dy.pojo.Orders;
import com.dy.user.User;
import com.dy.user.service.IUserService;
import com.kaoshi.common.config.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.dy.placeOrder.ConfirmOrder;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@RestController
@RequestMapping("placeOrder/confirm-order")
public class ConfirmOrderController {
    @Resource
    private ConfirmOrderService confirmOrderService;

    @Resource
    private OrdersService ordersService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IUserService userService;


    //根据id查询订单
    @RequestMapping("findById")
    public List<ConfirmOrder> findById(@RequestParam Integer id) {
        return confirmOrderService.findById(id);
    }


    //余额支付订单
    @RequestMapping("add1")
    public Result add1(@RequestBody ConfirmOrder confirmOrder) {
        confirmOrder.setPaymentTime(LocalDateTime.now());
        confirmOrder.setState(1);


        User byId = userService.getById(confirmOrder.getUid());
        //判断余额是否足够
        if (byId.getDoucoin().compareTo(confirmOrder.getTotalAmount()) >= 0){
            // 执行减法操作
            byId.setDoucoin(byId.getDoucoin().subtract(confirmOrder.getTotalAmount()));
            userService.updateById(byId);

            Orders orders = ordersService.getById(confirmOrder.getOid());
            orders.setStatus("待发货");
            ordersService.updateById(orders);

            confirmOrder.setState(2);

        }else{
            return Result.fail("余额不足");
        }

        confirmOrderService.save(confirmOrder);
        return Result.ok("支付成功");
    }

    //支付宝支付订单
    @RequestMapping("add2")
    public Result add2(@RequestBody ConfirmOrder confirmOrder) {
        confirmOrder.setPaymentTime(LocalDateTime.now());
        confirmOrder.setState(1);
        confirmOrderService.save(confirmOrder);

        if (confirmOrder.getOid() != null) {
            Orders byId = ordersService.getById(confirmOrder.getOid());
            byId.setSl(confirmOrder.getSl());
            byId.setStatus("待支付");
            ordersService.updateById(byId);
        }

        Integer id = confirmOrder.getId();
        // 确保ID不为null再设置到session
        if (id != null) {
            redisTemplate.opsForValue().set("cid", id);
        } else {
            System.out.println("id is null");
        }
        return Result.ok(id);
    }





    @GetMapping("listOrdersByStatus")
    public List<Orders> listOrders(@RequestParam String status) {
        return ordersService.listOrdersByStatus(status);
    }
    @PostMapping("listOrdersByBq")
    public List<Orders> listOrdersByBq(@RequestParam(value = "bq", required = true) String bq, @RequestBody List<Orders> orders) {
        System.out.println("____________________________"+bq+"__________");
        return ordersService.listOrdersByBq(bq, orders);
    }
    @PostMapping("changeStatus")
    public Result changeStatus(@RequestParam Integer oid, @RequestParam String status) {
        Orders orders = ordersService.getById(oid);
        orders.setStatus(status);
        ordersService.updateById(orders);
        return Result.ok();
    }
    @GetMapping("delete")
    public Result delete(Integer id) {
        System.out.println("删除订单id："+id);
        ordersService.removeById(id);
        return Result.ok();
    }

    @RequestMapping("duihuan")
    public Result duihuan(@RequestParam Integer uid,@RequestParam Integer yuan) {
        User byId = userService.getById(uid);
        byId.setIntegral(byId.getIntegral()-yuan);
        byId.setDoucoin(byId.getDoucoin().add(new BigDecimal(yuan/10000)));
        userService.updateById(byId);
        return Result.ok();
    }


}
